package SiTuQingYou.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.oreilly.servlet.MultipartRequest;

import SiTuQingYou.dao.PackageTourDao;
import SiTuQingYou.util.FileRename;
import SiTuQingYou.util.Pager;
import SiTuQingYou.vo.PackageTour;
import SiTuQingYou.vo.User;

/**
 * Servlet implementation class PackageTourServlet
 */
@WebServlet("/PackageTourServlet")
public class PackageTourServlet extends BaseAction {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PackageTourServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    //显示所有旅行团信息
   	protected void showAllPackageTour(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   		// TODO Auto-generated method stubr
   		Pager pager = Pager.init(request);
   		ArrayList<PackageTour> pat  = PackageTourDao.getptd().lookForAllPackageTour(pager);
   		request.setAttribute("pat",pat);
   	}
   	
   	//旅行团加入购物车
   	protected void addToCart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   		// TODO Auto-generated method stubr
   		String packageid = request.getParameter("id");
   		HttpSession session = request.getSession();
   		User user = (User)session.getAttribute("user");
   		if(user==null) {
   			response.sendRedirect("/SiTuQingYou/anotherLogReg/login.jsp");
   			return;
   		}
   		
   		PackageTourDao.getptd().addToShoppingCart(user.getId(),Integer.parseInt(packageid));
 
   		response.sendRedirect("/SiTuQingYou/index/packagetour.jsp?tobook=1");
   		
   	}
  //添加旅游团
   	protected void addpack(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    // TODO Auto-generated method stubr
   		String filepath = req.getServletContext().getRealPath("/");
		MultipartRequest re =new MultipartRequest(req, filepath+"images", 2*1024*1024, "utf-8",new FileRename());
		String title = re.getParameter("packname");
		String infomation = re.getParameter("packinfor");
		Float price =Float.parseFloat(re.getParameter("packpir"));
		Integer people = Integer.parseInt(re.getParameter("packpeo"));
		String year = re.getParameter("year");
		String month = re.getParameter("month");
		String day = re.getParameter("day");
		String hour = re.getParameter("hour");
		String minute = re.getParameter("minute");
		String s=year+"-"+month+"-"+day+" "+hour+":"+minute+":00";
		String packphoto = re.getFilesystemName("packphoto");
		Timestamp time = Timestamp.valueOf(s);
		PackageTour pack = new PackageTour();
		pack.setTitle(title);
		pack.setInfomation(infomation);
		pack.setPeople(people);
		pack.setPirce(price);
		pack.setPacktime(time);
		pack.setPhoto(packphoto);
		PackageTourDao.getptd().addpack(pack);
		PrintWriter out = resp.getWriter();
		out.print("添加完成!!!");
		
	}

	 @Override
	protected void doDefault(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
	    // TODO Auto-generated method stubr
		
	}//修改完成
	 protected void  mopack(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		    // TODO Auto-generated method stubr
		 	Integer id = Integer.parseInt(req.getParameter("id"));
		 	String title = req.getParameter("packname");
			String infomation = req.getParameter("packinfor");
			Float price =Float.parseFloat(req.getParameter("packpir"));
			Integer people = Integer.parseInt(req.getParameter("packpeo"));
			String time = req.getParameter("year");
			Timestamp times = Timestamp.valueOf(time);
			PackageTour pack = new PackageTour();
			pack.setId(id);
			pack.setInfomation(infomation);
			pack.setPacktime(times);
			pack.setTitle(title);
			pack.setPirce(price);
			pack.setPeople(people);
			PackageTourDao.getptd().mopack(pack);
			PrintWriter out = resp.getWriter();
			out.print("修改完成！！！");
		}
	 //查找一个
	 protected void onepack(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		    // TODO Auto-generated method stubr
	   		int  id = Integer.parseInt(req.getParameter("id"));
	   		PackageTour pack = PackageTourDao.getptd().onepack(id);
	   		req.setAttribute("pack", pack);
		}
	 //删除
	 protected void dele(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		    // TODO Auto-generated method stubr
		   	Integer id =Integer.parseInt(req.getParameter("id"));
		   	PackageTourDao.getptd().delete(id);
		}
}
